<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no,email=no" />
		<title>订单详情</title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/style.css" rel="stylesheet" />
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">订单详情</h1>
		</header>
		<div id="scrollDiv" class="scroll-div">
			<div class="mui-content transparent">
				<div id="orderList" style="margin: 10px 0;">
				</div>
			</div>
		</div>
		<div id="dcontent" class="dcontent"></div>
		
		<script src="js/mui.min.js"></script>
		<script src="js/app.js"></script>
		<script>
			var pays={},
				orderId,
				mask=null,
				ws,
				orderListPage;
			mui.init({
				swipeBack: true
			});
			
			mui.plusReady(function() {
				orderListPage=plus.webview.getWebviewById('order-list.html')
				ws=plus.webview.currentWebview();
				orderId=ws.orderId;
				ws.setStyle({scrollIndicator:'none'});
				var w=plus.nativeUI.showWaiting('',{padlock:true});
//				console.log(URL+'api/MemberOrder/GetOrderDetail/'+orderId)
				mui.ajax(URL+'api/MemberOrder/GetOrderDetail/'+orderId,{
					dataType:'json',
					type:'get',
					timeout:10000,
					success:function(data){
//						console.log(data)
						if(data.Success=='true'){
							var html='';
					        
					        html+='<ul class="mui-table-view mui-table-view-chevron address-list" id="addressList">'+
								'<li class="mui-table-view-cell">'+
									'<a class="mui-navigate-right" id="orderAddress">'+
										'<p><span>'+data.Order.ShipTo+'</span>'+data.Order.Phone+'</p>'+
										'<p><i class="mui-icon mui-icon-location"></i><span>'+data.Order.Address+'</span></p>'+
									'</a>'+
								'</li>'+
							'</ul>';
							if(data.Order.HasExpressStatus){
								html+='<div class="order-info" id="logistics">'+
									'<div class="item">'+
										'<div class="item-text express-box" id="expressStatus">'+
											'<div class="express-loading">'+
												'<div class="mui-pull-loading mui-spinner custom-loading"></div>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>';
			            	}
				            html+='<div class="order-info">'+
								'<div class="item">'+
									'<div class="order-shop">'+
									'<a class="vshop-url" data-id="'+data.Order.VShopId+'">'+data.Order.ShopName+'</a>'+
									'<span class="mui-pull-right">'+data.Order.Status+'</span></div>'+
								'</div>';
				            	for(var j=0;j<data.OrderItem.length;j++){
					                html+='<div class="item">'+
					                    '<div class="buy-goods">'+
					                        '<a class="goods-url" data-id="'+data.OrderItem[j].ProductId+'"><img src="'+data.OrderItem[j].ProductImage+'"/></a>'+
					                        '<p><em>¥ '+data.OrderItem[j].Price+'</em><span><a class="goods-url" data-id="'+data.OrderItem[j].ProductId+'">'+data.OrderItem[j].ProductName+'</a></span></p>'+
					                        '<h5><span>x '+data.OrderItem[j].Count+'</span>';
					                        	if(data.OrderItem[j].size!='')
													html += '尺寸：'+data.OrderItem[j].size+'&nbsp;&nbsp;';
												if(data.OrderItem[j].color!='')
													html += '颜色：'+data.OrderItem[j].color+'&nbsp;&nbsp;';
												if(data.OrderItem[j].version!='')
													html += '规格：'+data.OrderItem[j].version;
					                            html+='</h5>'+
					                    '</div>'+
					                '</div>';
				               	}
					            html+='<div class="item">'+
					            	'<div class="item-text">'+
				                    '&nbsp;<span class="mui-pull-right order-total">共'+data.OrderItem.length+'件商品 &nbsp;&nbsp; 总金额：<em>¥ '+data.Order.RealTotalAmount+'</em></span>'+
					            	'</div>'+
					            '</div>'+
					            '<div class="item">'+
					            	'<div class="item-text">'+
				                    '订单号：'+data.Order.Id+' '+
					            	'</div>'+
					            '</div>'+
					            '<div class="item">'+
					            	'<div class="item-text">'+
				                    '创建时间：'+data.Order.OrderDate+' '+
					            	'</div>'+
					            '</div>';
					            if(data.Order.orderStatus==1){
					            	html+='<div class="item total-m item-btn" data-amout="'+data.Order.RealTotalAmount+'">'+
					            		'<button class="mui-btn cancelOrder">取消订单</button>'+
					            		'<button class="mui-btn mui-btn-negative payOrder">付款</button>'+
					            	'</div>';
					            }
					            if(data.Order.orderStatus==3){
					            	html+='<div class="item total-m item-btn">'+
					            		'<button class="mui-btn mui-btn-negative enterOrder">确认收货</button>'+
					            	'</div>';
					            }
					            if(data.Order.orderStatus==5){
						            	if(data.Order.commentCount==0){
							            	/*html+='<div class="item total-m item-btn">'+
							            		'<button class="mui-btn mui-btn-negative evaluateOrder">评价</button>'+
							            	'</div>';*/
						            	}
					            }
					            
					        html+='</div>';
					        
				            document.getElementById('orderList').innerHTML=html;
				            
				            if(data.Order.HasExpressStatus){
//				            	console.log(URL+'api/MemberOrder/GetExpressInfo?orderId='+orderId)
			            		mui.ajax(URL+'api/MemberOrder/GetExpressInfo?orderId='+orderId,{
									dataType:'json',
									type:'get',
									timeout:10000,
									success:function(dataE){
//										console.log(dataE)
										
										if(dataE.Success=='true'){
											var express='';
											if(dataE.Comment.Success){
												for(var k=0; k<dataE.Comment.Data.length; k++){
													express+='<p>'+dataE.Comment.Data[k].time+'<span>'+dataE.Comment.Data[k].content +'</span></p>';
												}
											}else{
												express+='<p class="red">'+dataE.Comment.Msg+'</p>';
											}
											express+='运单号码：'+dataE.ExpressNum+' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 物流公司： '+dataE.ExpressCompanyName;
											
											document.getElementById('expressStatus').innerHTML=express;
										}
									},
									error:function(d){
//										console.log(d)
										var $logistics = document.getElementById("logistics")
										document.getElementById('orderList').removeChild($logistics)
										
									}
								});
			            	}
							w.close();
							document.getElementsByClassName("mui-content")[0].className="mui-content";
						}
					},
					error:function(xhr,type,errorThrown){
						w.close();
						reloadWvLoad();
					}
				});
				
				plus.payment.getChannels(function(channels){
					var content=document.getElementById('dcontent');
					for(var i in channels){
						var channel=channels[i];
						if(channel.id=='alipay' /*|| channel.id=='wxpay'*/){
						pays[channel.id]=channel;
						var de=document.createElement('div');
						de.setAttribute('class','custom-btn');
						de.setAttribute('onclick','pay(this.id)');
						de.id=channel.id;
						de.innerText=channel.description+"支付";
						content.appendChild(de);
						checkServices(channel);
						}
					}
				},function(e){
					plus.nativeUI.alert("获取支付方式失败："+e.message);
				});
				
			});
			
			
			mui('#orderList').on('tap', '.vshop-url', function() {
				var vshopid = this.getAttribute('data-id');
				if(vshopid==null){
					plus.nativeUI.toast('该店铺暂未开通微店')
				}else{
					showVshop(vshopid,'/')
				}
			});
			
			mui('#orderList').on('tap', '.goods-url', function() {
				var id = this.getAttribute('data-id');
				showProduct(id);
			});
			
			
			
			mui('#orderList').on('tap', '.enterOrder', function() {
				var btnArray = ['取消', '确认'];
				mui.confirm('请确保您已收到宝贝,再收货！', '', btnArray, function(e) {
					if (e.index == 1) {
				    	mui.ajax(URL+'api/MemberOrder/PostConfirmOrder',{
							data:JSON.stringify({orderId:orderId}),
							dataType:'json',
							type:'POST',
							contentType:'application/json',
							timeout:10000,
							success:function(data){
								if(data.Success=="true"){
									plus.nativeUI.toast('已确认收货');
									mui.fire(orderListPage,'updateData');
									ws.close();
								}else{
									plus.nativeUI.toast('确认收货失败，请稍后重试')
								}
							},
							error:function(xhr,type,errorThrown){
								plus.nativeUI.toast('请求失败，请检查网络')
							}
						});
					}
				});
			});
			
			
			mui('#orderList').on('tap', '.cancelOrder', function() {
				var btnArray = ['取消', '确认'];
				mui.confirm('确定取消该订单？', '', btnArray, function(e) {
					if (e.index == 1) {
				    	mui.ajax(URL+'api/MemberOrder/PostCloseOrder',{
							data:JSON.stringify({orderId:orderId}),
							dataType:'json',
							type:'POST',
							contentType:'application/json',
							timeout:10000,
							success:function(data){
								if(data.Success=="true"){
									plus.nativeUI.toast('订单取消成功');
									mui.fire(orderListPage,'updateData');
									ws.close();
								}else{
									plus.nativeUI.toast('订单取消失败，请稍后重试')
								}
							},
							error:function(xhr,type,errorThrown){
								plus.nativeUI.toast('订单取消失败，请检查网络')
							}
						});
					}
				});
			});
			mui('#orderList').on('tap', '.payOrder', function() {
				if(this.parentNode.getAttribute('data-amout')<=0){
					var pW=plus.nativeUI.showWaiting();
					mui.ajax(URL+'api/Order/GetPayOrderByIntegral?orderIds='+orderId,{
						dataType:'json',
						type:'get',
						timeout:10000,
						success:function(data){
							if(data.Success=="true"){
								pw.close();
								plus.nativeUI.alert('订单需支付金额为零，购买成功,等待发货');
								mui.fire(orderListPage,'updateData');
								ws.close();
							}else{
								pw.close();
								plus.nativeUI.toast('订单支付失败，请稍后重试')
							}
						},
						error:function(xhr,type,errorThrown){
							pw.close();
							plus.nativeUI.toast('订单支付失败，请检查网络')
						}
					});
					
				}else{
					if(!mask)
						mask = mui.createMask(function(){document.getElementById('dcontent').style.display='none'});
					mask.show();
					document.getElementById('dcontent').className='dcontent active';
				}
			});
			
			// 检测是否安装支付服务
			function checkServices(pc){
				if(!pc.serviceReady){
					var txt=null;
					switch(pc.id){
						case "alipay":
						txt="检测到系统未安装“支付宝快捷支付服务”，无法完成支付操作，是否立即安装？";
						break;
						default:
						txt="系统未安装“"+pc.description+"”服务，无法完成支付，是否立即安装？";
						break;
					}
					plus.nativeUI.confirm(txt,function(e){
						if(e.index==0){
							pc.installService();
						}
					},pc.description);
				}
			}
			
			var wPay=null;
			var PAYSERVER=URL+'api/payment/GetPaymentList?orderIds=';
			// 支付
			function pay(id){
				if(wPay){return;}//检查是否请求订单中
				//var url=PAYSERVER;
				if(id=='alipay'||id=='wxpay'){
					//url+=id;
				}else{
					plus.nativeUI.alert("不支持此支付通道！");
					return;
				}
				wPay=plus.nativeUI.showWaiting();
				
				// 请求支付订单
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					switch(xhr.readyState){
						case 4:
						wPay.close();wPay=null;
						if(xhr.status==200){
							var order=JSON.parse(xhr.responseText);
//							console.log(order)
							plus.payment.request(pays[id],order[0].url,function(result){
								plus.nativeUI.alert("支付成功，等待卖家发货！",function(){
									mui.fire(orderListPage,'updateData');
									ws.close();
								});
							},function(e){
								mask.close();
								document.getElementById('dcontent').style.display='none';
								plus.nativeUI.alert("订单支付失败！请重新支付");
							});
						}else{
							plus.nativeUI.alert("获取订单信息失败！");
						}
						break;
						default:
						break;
					}
				}
				xhr.open('GET',PAYSERVER+orderId);
				xhr.send();
			}
		</script>
	</body>

</html>